﻿#if 0
#define _CRT_SECURE_NO_WARNINGS

#define STB_TRUETYPE_IMPLEMENTATION

#include <crtdbg.h>
#include "font.h"


CFont::CFont()
{
	m_textColor.r = 255;
	m_textColor.g = 255;
	m_textColor.b = 255;

	m_backColor.r = 128;
	m_backColor.g = 128;
	m_backColor.b = 128;

	FILE* fp = 0;

	fp = fopen("nanum.ttf", "rb");
	if (!fp) _ASSERT(0);

	fseek(fp, 0, SEEK_END);
	int size = ftell(fp);
	fseek(fp, 0, SEEK_SET);

	m_fontBuffer = new unsigned char[size];
	m_bmap = new unsigned char [512*512];

	fread(m_fontBuffer, 1, size, fp);
	fclose(fp);
}

CFont::~CFont()
{
	delete [] m_fontBuffer;
}

void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B)
{
	Uint32 color = SDL_MapRGB(screen->format, R, G, B);

	switch (screen->format->BytesPerPixel)
	{
	case 1: // Assuming 8-bpp
		{
			Uint8 *bufp;
			bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
			*bufp = color;
		}
		break;
	case 2: // Probably 15-bpp or 16-bpp
		{
			Uint16 *bufp;
			bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
			*bufp = color;
		}
		break;
	case 3: // Slow 24-bpp mode, usually not used
		{
			Uint8 *bufp;
			bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
			if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
			{
				bufp[0] = color;
				bufp[1] = color >> 8;
				bufp[2] = color >> 16;
			} else {
				bufp[2] = color;
				bufp[1] = color >> 8;
				bufp[0] = color >> 16;
			}
		}
		break;
	case 4: // Probably 32-bpp
		{
			Uint32 *bufp;
			bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
			*bufp = color;
		}
		break;
	}
}

//void	CFont::Render(wchar_t *msg, int method)
void	CFont::Render(char *msg, int method)
{
	stbtt_bakedchar g_cdata[96]; // ASCII 32..126 is 95 glyphs

	float pixel_height = 15.f;
	stbtt_BakeFontBitmap(m_fontBuffer, 0, pixel_height, m_bmap, 512, 512, 32, 96, g_cdata);

	switch (method)
	{
	case FONT_SOLID :
	case FONT_SHADED :
	case FONT_BLENDED :
		{

			SDL_Rect offset;
			//offset.x = x;
			//offset.y = y;

			int msgwidth = getTextLength(g_cdata, msg);

			int width = msgwidth;
			int height = 15;

			SDL_Surface *fontsurface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, 0,0,0,0);
			SDL_LockSurface(fontsurface);

			float x,y;
			x = 0;
			y = 0;

			float max_y = 0;

			while(*msg)
			{
				stbtt_aligned_quad q;

				int c = (unsigned char)*msg;
				if (c >= 32 && c < 128)
				{	
					getBakedQuad(g_cdata, 512,512, c-32, &x,&y,&q);

					x=x;
					y=y;
				}

				++msg;
			}
			
// 			DrawPixel(fontsurface, )
// 			m_bmap = new unsigned char [512*512];

			SDL_UnlockSurface(fontsurface);

			//fontsurface->pixels

			// int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect);
			//SDL_BlitSurface( fontsurface, NULL, m_surface, &offset );

			

/*
	while (*text)
	{
		int c = (unsigned char)*text;
		if (c >= 32 && c < 128)
		{			
			stbtt_aligned_quad q;
			getBakedQuad(g_cdata, 512,512, c-32, &x,&y,&q);

			glTexCoord2f(q.s0, q.t0);
			glVertex2f(q.x0, q.y0);
			glTexCoord2f(q.s1, q.t1);
			glVertex2f(q.x1, q.y1);
			glTexCoord2f(q.s1, q.t0);
			glVertex2f(q.x1, q.y0);

			glTexCoord2f(q.s0, q.t0);
			glVertex2f(q.x0, q.y0);
			glTexCoord2f(q.s0, q.t1);
			glVertex2f(q.x0, q.y1);
			glTexCoord2f(q.s1, q.t1);
			glVertex2f(q.x1, q.y1);
		}
		++text;
	}
*/


		}
		break;
	}

}

float CFont::getTextLength(stbtt_bakedchar *chardata, const char* text)
{
	float xpos = 0;
	float len = 0;
	while (*text)
	{
		int c = (unsigned char)*text;
		if (c >= 32 && c < 128)
		{
			stbtt_bakedchar *b = chardata + c-32;
			int round_x = STBTT_ifloor((xpos + b->xoff) + 0.5);
			len = round_x + b->x1 - b->x0 + 0.5f;
			xpos += b->xadvance;
		}
		++text;
	}

	return len;
}


void CFont::getBakedQuad(stbtt_bakedchar *chardata, int pw, int ph, int char_index, float *xpos, float *ypos, stbtt_aligned_quad *q)
{
	stbtt_bakedchar *b = chardata + char_index;
	int round_x = STBTT_ifloor(*xpos + b->xoff);
	int round_y = STBTT_ifloor(*ypos - b->yoff);

	q->x0 = (float)round_x;
	q->y0 = (float)round_y;
	q->x1 = (float)round_x + b->x1 - b->x0;
	q->y1 = (float)round_y - b->y1 + b->y0;

	q->s0 = b->x0 / (float)pw;
	q->t0 = b->y0 / (float)pw;
	q->s1 = b->x1 / (float)ph;
	q->t1 = b->y1 / (float)ph;

	*xpos += b->xadvance;
}


#else

#include "font.h"


CFont::CFont()
{
	if( TTF_Init() == -1 )
	{
		return;
	}

	m_textColor.r = 255;
	m_textColor.g = 255;
	m_textColor.b = 255;

	m_backColor.r = 128;
	m_backColor.g = 128;
	m_backColor.b = 128;

	m_font = TTF_OpenFont("nanum.ttf", 14 );
}


CFont::~CFont()
{
	TTF_CloseFont( m_font );
	TTF_Quit();
}

void	CFont::Render(wchar_t *msg, int method)
{
	switch (method)
	{
	case FONT_SOLID :
		m_surface = TTF_RenderUNICODE_Solid(m_font, (const Uint16 *)msg, m_textColor);
		break;

	case FONT_SHADED :
		m_surface = TTF_RenderUNICODE_Shaded(m_font, (const Uint16 *)msg, m_textColor, m_backColor);
		break;

	case FONT_BLENDED :
		m_surface = TTF_RenderUNICODE_Blended(m_font, (const Uint16 *)msg, m_textColor);
		break;
	}
}

#endif

